<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2020/5/12
 * Time: 16:37
 */

namespace App\Repositories;

use App\Patent;
use App\Repositories\Eloquent\Repository;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;

class PatentRepository extends Repository
{
    function model()
    {
        return Patent::class;
    }

    public function typeStatistic($user, $role)
    {
        return $this->model
            ->select(DB::raw('count(*) as count,patent_type_id'))
            ->when(!in_array($role, [1, 2]), function ($query) use ($user) {
                $query->where('user_id', $user['id']);
            })->when($role == 2, function ($query) use ($user) {
                $query->where('department_id', $user['department_id']);
            })
            ->groupBy('patent_type_id')
            ->pluck('count', 'patent_type_id');
    }

    public function legalStatusStatistic($user, $role)
    {
        return $this->model
            ->select(DB::raw('count(*) as count,IFNULL(legal_status,"其他") as legal_status'))
            ->when(!in_array($role, [1, 2]), function ($query) use ($user) {
                $query->where('user_id', $user['id']);
            })->when($role == 2, function ($query) use ($user) {
                $query->where('department_id', $user['department_id']);
            })
            ->groupBy('legal_status')
            ->pluck('count', 'legal_status');
    }

    public function applyDateStatistic($user, $role): \Illuminate\Support\Collection
    {
        $year = Carbon::now()->year - 10;
        return $this->model->newQuery()
            ->select(DB::raw('count(*) as count,YEAR(apply_date) as date'))
            ->when(!in_array($role, [1, 2]), function ($query) use ($user) {
                $query->where('user_id', $user['id']);
            })->when($role == 2, function ($query) use ($user) {
                $query->where('department_id', $user['department_id']);
            })
            ->whereYear('apply_date', '>=', $year)
            ->groupBy('date')
            ->orderByDesc('date')
            ->pluck('count', 'date');
    }

    public function shouQuanStatistic($year_num): Collection
    {
        $year = Carbon::now()->year - $year_num;
        return $this->model
            ->select(DB::raw('count(*) as count,YEAR(publication_date) as year,patent_type_id'))
            ->whereYear('publication_date', '>', $year)
            ->orderByDesc('patent_type_id')
            ->groupBy(['patent_type_id', 'year'])
            ->get();
    }

    public function applyShouQuanStatistic($year): Collection
    {
        return $this->model
            ->select(DB::raw('count(*) as count,YEAR(apply_date) as a_year,YEAR(publication_date) as p_year,patent_type_id'))
            ->where(function ($query) use ($year) {
                $query->whereYear('publication_date', '>=', $year)->orWhereYear('apply_date', '>=', $year);
            })
            ->groupBy(['a_year', 'p_year', 'patent_type_id'])
            ->get();
    }


}
